package ch.uzh.ifi.seal.ssqa.model.diagram.part;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;

import ch.uzh.ifi.seal.ssqa.model.ModelPackage;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramComposite2EditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramCompositeDiagramCompositeChildrenCompartment2EditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramCompositeDiagramCompositeChildrenCompartmentEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramCompositeEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramCompositeName2EditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramCompositeNameEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramFolder2EditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramFolderDiagramFolderChildrenCompartment2EditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramFolderDiagramFolderChildrenCompartmentEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramFolderEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramFolderName2EditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramFolderNameEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramModule2EditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramModuleDiagramModuleChildrenCompartment2EditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramModuleDiagramModuleChildrenCompartmentEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramModuleEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramModuleName2EditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramModuleNameEditPart;
import ch.uzh.ifi.seal.ssqa.model.diagram.edit.parts.DiagramVComEditPart;

/**
 * This registry is used to determine which type of visual object should be
 * created for the corresponding Diagram, Node, ChildNode or Link represented
 * by a domain model object.
 * 
 * @generated
 */
public class SsqaVisualIDRegistry {

	/**
	 * @generated
	 */
	private static final String DEBUG_KEY = "ch.uzh.ifi.seal.ssqa.diagram/debug/visualID"; //$NON-NLS-1$

	/**
	 * @generated
	 */
	public static int getVisualID(View view) {
		if (view instanceof Diagram) {
			if (DiagramEditPart.MODEL_ID.equals(view.getType())) {
				return DiagramEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		return ch.uzh.ifi.seal.ssqa.model.diagram.part.SsqaVisualIDRegistry
				.getVisualID(view.getType());
	}

	/**
	 * @generated
	 */
	public static String getModelID(View view) {
		View diagram = view.getDiagram();
		while (view != diagram) {
			EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
			if (annotation != null) {
				return (String) annotation.getDetails().get("modelID"); //$NON-NLS-1$
			}
			view = (View) view.eContainer();
		}
		return diagram != null ? diagram.getType() : null;
	}

	/**
	 * @generated
	 */
	public static int getVisualID(String type) {
		try {
			return Integer.parseInt(type);
		} catch (NumberFormatException e) {
			if (Boolean.TRUE.toString().equalsIgnoreCase(
					Platform.getDebugOption(DEBUG_KEY))) {
				SsqaDiagramEditorPlugin.getInstance().logError(
						"Unable to parse view type as a visualID number: "
								+ type);
			}
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static String getType(int visualID) {
		return Integer.toString(visualID);
	}

	/**
	 * @generated
	 */
	public static int getDiagramVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		if (ModelPackage.eINSTANCE.getDiagram().isSuperTypeOf(
				domainElement.eClass())
				&& isDiagram((ch.uzh.ifi.seal.ssqa.model.Diagram) domainElement)) {
			return DiagramEditPart.VISUAL_ID;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static int getNodeVisualID(View containerView, EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		String containerModelID = ch.uzh.ifi.seal.ssqa.model.diagram.part.SsqaVisualIDRegistry
				.getModelID(containerView);
		if (!DiagramEditPart.MODEL_ID.equals(containerModelID)) {
			return -1;
		}
		int containerVisualID;
		if (DiagramEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = ch.uzh.ifi.seal.ssqa.model.diagram.part.SsqaVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = DiagramEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		switch (containerVisualID) {
		case DiagramEditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getDiagramFolder().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramFolderEditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getDiagramModule().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramModuleEditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getDiagramComposite().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramCompositeEditPart.VISUAL_ID;
			}
			break;
		case DiagramFolderDiagramFolderChildrenCompartmentEditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getDiagramFolder().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramFolder2EditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getDiagramModule().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramModule2EditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getDiagramComposite().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramComposite2EditPart.VISUAL_ID;
			}
			break;
		case DiagramFolderDiagramFolderChildrenCompartment2EditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getDiagramFolder().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramFolder2EditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getDiagramModule().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramModule2EditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getDiagramComposite().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramComposite2EditPart.VISUAL_ID;
			}
			break;
		case DiagramModuleDiagramModuleChildrenCompartmentEditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getDiagramFolder().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramFolder2EditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getDiagramModule().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramModule2EditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getDiagramComposite().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramComposite2EditPart.VISUAL_ID;
			}
			break;
		case DiagramCompositeDiagramCompositeChildrenCompartmentEditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getDiagramFolder().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramFolder2EditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getDiagramModule().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramModule2EditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getDiagramComposite().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramComposite2EditPart.VISUAL_ID;
			}
			break;
		case DiagramModuleDiagramModuleChildrenCompartment2EditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getDiagramFolder().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramFolder2EditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getDiagramModule().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramModule2EditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getDiagramComposite().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramComposite2EditPart.VISUAL_ID;
			}
			break;
		case DiagramCompositeDiagramCompositeChildrenCompartment2EditPart.VISUAL_ID:
			if (ModelPackage.eINSTANCE.getDiagramFolder().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramFolder2EditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getDiagramModule().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramModule2EditPart.VISUAL_ID;
			}
			if (ModelPackage.eINSTANCE.getDiagramComposite().isSuperTypeOf(
					domainElement.eClass())) {
				return DiagramComposite2EditPart.VISUAL_ID;
			}
			break;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static boolean canCreateNode(View containerView, int nodeVisualID) {
		String containerModelID = ch.uzh.ifi.seal.ssqa.model.diagram.part.SsqaVisualIDRegistry
				.getModelID(containerView);
		if (!DiagramEditPart.MODEL_ID.equals(containerModelID)) {
			return false;
		}
		int containerVisualID;
		if (DiagramEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = ch.uzh.ifi.seal.ssqa.model.diagram.part.SsqaVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = DiagramEditPart.VISUAL_ID;
			} else {
				return false;
			}
		}
		switch (containerVisualID) {
		case DiagramEditPart.VISUAL_ID:
			if (DiagramFolderEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DiagramModuleEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DiagramCompositeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case DiagramFolderEditPart.VISUAL_ID:
			if (DiagramFolderNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DiagramFolderDiagramFolderChildrenCompartmentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case DiagramModuleEditPart.VISUAL_ID:
			if (DiagramModuleNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DiagramModuleDiagramModuleChildrenCompartment2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case DiagramCompositeEditPart.VISUAL_ID:
			if (DiagramCompositeNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DiagramCompositeDiagramCompositeChildrenCompartment2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case DiagramFolder2EditPart.VISUAL_ID:
			if (DiagramFolderName2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DiagramFolderDiagramFolderChildrenCompartment2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case DiagramModule2EditPart.VISUAL_ID:
			if (DiagramModuleName2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DiagramModuleDiagramModuleChildrenCompartmentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case DiagramComposite2EditPart.VISUAL_ID:
			if (DiagramCompositeName2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DiagramCompositeDiagramCompositeChildrenCompartmentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case DiagramFolderDiagramFolderChildrenCompartmentEditPart.VISUAL_ID:
			if (DiagramFolder2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DiagramModule2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DiagramComposite2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case DiagramFolderDiagramFolderChildrenCompartment2EditPart.VISUAL_ID:
			if (DiagramFolder2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DiagramModule2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DiagramComposite2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case DiagramModuleDiagramModuleChildrenCompartmentEditPart.VISUAL_ID:
			if (DiagramFolder2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DiagramModule2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DiagramComposite2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case DiagramCompositeDiagramCompositeChildrenCompartmentEditPart.VISUAL_ID:
			if (DiagramFolder2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DiagramModule2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DiagramComposite2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case DiagramModuleDiagramModuleChildrenCompartment2EditPart.VISUAL_ID:
			if (DiagramFolder2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DiagramModule2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DiagramComposite2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case DiagramCompositeDiagramCompositeChildrenCompartment2EditPart.VISUAL_ID:
			if (DiagramFolder2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DiagramModule2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (DiagramComposite2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		}
		return false;
	}

	/**
	 * @generated
	 */
	public static int getLinkWithClassVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		if (ModelPackage.eINSTANCE.getDiagramVCom().isSuperTypeOf(
				domainElement.eClass())) {
			return DiagramVComEditPart.VISUAL_ID;
		}
		return -1;
	}

	/**
	 * User can change implementation of this method to handle some specific
	 * situations not covered by default logic.
	 * 
	 * @generated
	 */
	private static boolean isDiagram(ch.uzh.ifi.seal.ssqa.model.Diagram element) {
		return true;
	}

}
